/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ws.converter;

import entidade.persistencia.AreaAtuacao;
import java.net.URI;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;
import entidade.persistencia.Empresa;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author victorinox
 */

@XmlRootElement(name = "areaAtuacao")
public class AreaAtuacaoConverter {
    private AreaAtuacao entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of AreaAtuacaoConverter */
    public AreaAtuacaoConverter() {
        entity = new AreaAtuacao();
    }

    /**
     * Creates a new instance of AreaAtuacaoConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public AreaAtuacaoConverter(AreaAtuacao entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getAreaAtuacaoId() + "/").build() : uri;
        this.expandLevel = expandLevel;
        getEmpresaCollection();
    }

    /**
     * Creates a new instance of AreaAtuacaoConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public AreaAtuacaoConverter(AreaAtuacao entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for areaAtuacaoId.
     *
     * @return value for areaAtuacaoId
     */
    @XmlElement
    public Integer getAreaAtuacaoId() {
        return (expandLevel > 0) ? entity.getAreaAtuacaoId() : null;
    }

    /**
     * Setter for areaAtuacaoId.
     *
     * @param value the value to set
     */
    public void setAreaAtuacaoId(Integer value) {
        entity.setAreaAtuacaoId(value);
    }

    /**
     * Getter for nome.
     *
     * @return value for nome
     */
    @XmlElement
    public String getNome() {
        return (expandLevel > 0) ? entity.getNome() : null;
    }

    /**
     * Setter for nome.
     *
     * @param value the value to set
     */
    public void setNome(String value) {
        entity.setNome(value);
    }

    /**
     * Getter for empresaCollection.
     *
     * @return value for empresaCollection
     */
    @XmlElement
    public EmpresasConverter getEmpresaCollection() {
        if (expandLevel > 0) {
            if (entity.getEmpresaCollection() != null) {
                return new EmpresasConverter(entity.getEmpresaCollection(), uri.resolve("empresaCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for empresaCollection.
     *
     * @param value the value to set
     */
    public void setEmpresaCollection(EmpresasConverter value) {
        entity.setEmpresaCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the AreaAtuacao entity.
     *
     * @return an entity
     */
    @XmlTransient
    public AreaAtuacao getEntity() {
        if (entity.getAreaAtuacaoId() == null) {
            AreaAtuacaoConverter converter = UriResolver.getInstance().resolve(AreaAtuacaoConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved AreaAtuacao entity.
     *
     * @return an resolved entity
     */
    public AreaAtuacao resolveEntity(EntityManager em) {
        List<Empresa> empresaCollection = entity.getEmpresaCollection();
        List<Empresa> newempresaCollection = new java.util.ArrayList<Empresa>();
        for (Empresa item : empresaCollection) {
            newempresaCollection.add(em.getReference(Empresa.class, item.getEmpresaId()));
        }
        entity.setEmpresaCollection(newempresaCollection);
        return entity;
    }
}
